import React, { useState, useEffect } from 'react';
import { View } from 'react-native';
import { Toast } from '@ant-design/react-native';
import moment from 'moment';
import DataStatisticsView from '../../components/DataStatisticsView/index,';
import { selectDaily, impTypeCount } from '../../comconfig/sqlfun';

const index = ({ route }) => {
    const [dailyData, setDailyData] = useState([]);
    const [impCount, setImpCount] = useState([]);
    const getDailyData = async (date) => {
        try {
            const list = await selectDaily(date);
            const arr = [];
            for (let i = 0; i < list.length; i += 1) {
                arr.push(JSON.parse(list[i]));
            }
            setDailyData(arr);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    const getImpCount = async () => {
        try {
            const list = await impTypeCount();
            const arr = [];
            for (let i = 0; i < list.length; i += 1) {
                arr.push(JSON.parse(list[i]));
            }
            setImpCount(arr);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    useEffect(() => {
        getDailyData((moment(new Date()).format('YYYY-MM')));
        getImpCount();
    }, [route]);
    return (
        <View>
            <DataStatisticsView
                getDailyData={getDailyData}
                dailyData={dailyData}
                impCount={impCount}
            />
        </View>
    );
};
export default index;
